import { ISJSON } from './baseMethods';

/**
 * @exports setLocal 自定义赋值token
 * @param param 自定义的标识符
 * @param value 任意值
 */
const setLocal = (param: string, value: unknown) => {
  let state: any = value;
  if (!ISJSON(value)) {
    state = JSON.stringify(value);
  }
  localStorage.setItem(param, state);
};
/**
 * @exports getLocal 获取自定义localStorage中的值
 * @param   必填项,自定义保存在localStorage中的标识符
 * @return  localStorage中的标识符对应的值
 */
const getLocal = (param: string): boolean | unknown => {
  let state: any = localStorage.getItem(param);
  if (ISJSON(state)) {
    state = JSON.parse(state);
  }
  return state;
};
/**
 * @exports clearLocalT 删除localStorage中所有的数据,并刷新页面
 */
const clearLocal = () => {
  localStorage.clear();
  location.reload();
};

/**
 * @exports isLocal 判断在localStorage是否存在 [param] 想对应的值, 存在 =>true、不存在 =>false
 * @param param 必填项,自定义保存在localStorage中的标识符
 * @return true|false
 */
const isLocal = (param: string): boolean => {
  let state = true;
  if (param) {
    state = !!localStorage.getItem(param);
  }
  return state;
};

export { isLocal, getLocal, setLocal, clearLocal };
